﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Prism.Modularity;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Prism.Regions;
using ExplorerInfrastructure;
using BBInterfaceNET.ProjectExplorer.ViewModels;
using Microsoft.Practices.Prism.Events;
using BBInterfaceNET.ProjectExplorer.Views;
using BBInterfaceNET.Events;

namespace BBInterfaceNET.ProjectExplorer.ModuleDefinition
{
    public class ProjectExplorerModule:IModule
    {
        IUnityContainer container;
        IEventAggregator eventAggregator;

        public ProjectExplorerModule(IUnityContainer container, IEventAggregator eventAggregator)
        {
            this.eventAggregator = eventAggregator;
            this.container = container;
        }
        public void Initialize()
        {
            IEventAggregator eventAggregator = container.Resolve<IEventAggregator>();
            container.RegisterInstance<IProjectExplorer>(new ExplorerViewModel(eventAggregator));
            IRegionManager regionManager=container.Resolve<IRegionManager>();
            regionManager.RegisterViewWithRegion("ExplorerRegion", typeof(ExplorerView));
            SubscribeToExplorerEvents();
        }

        private void SubscribeToExplorerEvents()
        {
            IProjectExplorer explorer = container.Resolve<IProjectExplorer>();
            explorer.ItemAdded += (s, args) =>
            {

            };
            explorer.ItemCreated += (s, args) =>
            {
                eventAggregator.GetEvent<FileCreatedEvent>().Publish(args.FilePath);
            };
            explorer.ItemDeleted += (s, args) =>
            {
                eventAggregator.GetEvent<FileDeletedEvent>().Publish(args.FilePath);
            };
            explorer.ItemOpened += (s, args) =>
            {
                eventAggregator.GetEvent<FileOpenedEvent>().Publish(args.FilePath);
            };
            explorer.ItemRenamed += (s, args) =>
            {
                eventAggregator.GetEvent<FileRenamedEvent>().Publish(new FileDataInfo() { OldPath = args.OldPath, NewPath = args.NewPath });
            };
        }
    }
}
